package cn.buddha.jesdp.proxy;

import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.cglib.proxy.Proxy;

import java.lang.reflect.Method;

/**
 * @ClassName JavaDynamicProxy
 * @Author 友野浩二
 * @Date 2020/3/9 22:39
 * @Description
 * @Version 1.0
 */
public class JavaDynamicProxy {

    public static void main(String[] args) {
        proxyTest();
    }

    private static void proxyTest() {
        JavaDeveloper tong = new JavaDeveloper("Tong");

        ClassLoader classLoader = tong.getClass().getClassLoader();
        Class<?>[] classes = tong.getClass().getInterfaces();

        MyAspect myAspect = new MyAspect();

        Developer tongProxy = (Developer) Proxy.newProxyInstance(classLoader, classes,
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                        myAspect.before();
                        Object invoke = method.invoke(tong, objects);
                        myAspect.after();
                        return invoke;
                    }
                });

        tongProxy.code();
        tongProxy.debug();
    }

}
